﻿/**
 * Copyright (c) 2008 michiyasu wada
 * http://www.seyself.com/
 * 
 * Distributed under The MIT License.
 * [http://www.opensource.org/licenses/mit-license.php]
 */

 
import flash.events.EventDispatcher;
import flash.events.Event;

class com.seyself.events.EventMediator extends EventDispatcher
{
    
    public var member:Array;
    
    private var eventer:EventDispatcher;
    
    public function EventMediator( eventer:Object )
    {
        member = [];
        this.eventer = eventer || new EventDispatcher();
    }
    
    public function addEventListener(type:String, listener:Function, 
        useCapture:Boolean, priority:Number, useWeakReference:Boolean):Void
    {
        var loc = this;
        var handler = function( event:Event )
        {
            var _member:Array = loc.member;
            var len:Number = _member.length;
            for (var i = 0; i < len;i++ ) {
                if( listener ) _member[i].addEventListener( type, listener, useCapture, priority, useWeakReference);
                _member[i].dispatchEvent( event );
            }
        }
        eventer.addEventListener( type, handler, useCapture, priority, useWeakReference);
    }
    
    public function hasEventListener(type:String):Boolean
    {
        return eventer.hasEventListener( type );
    }
    
    public function removeEventListener(
        type:String, listener:Function, useCapture:Boolean):Void
    {
        var len:Number = member.length;
        for (var i = 0; i < len;i++ ) {
            member[i].removeEventListener( type, listener, useCapture );
        }
        eventer.removeEventListener( type, listener, useCapture );
    }
    
    public function toString():String
    {
        return "[object EventMediator]";
    }
    
    
}
